﻿using Microsoft.Xna.Framework;
using Terraria;
using Terraria.GameContent.UI.Elements;
using Terraria.ModLoader;
using Terraria.UI;

namespace Bundlesone.Content.Items
{
    public class BundleUIState : UIState
    {
        public UIPanel MainPanel;
        public VanillaItemSlotWrapper[] Slots = new VanillaItemSlotWrapper[12];
        public Item[] StoredItems;
        private bool _dragging;
        private Vector2 _offset;
        private UIText _titleText;
        public void SetStorage(Item[] items)
        {
            StoredItems = items;            
            if (Slots[0] != null && StoredItems != null)
            {
                for (int i = 0; i < 12; i++)
                {
                    Slots[i].Item = StoredItems[i].Clone();
                }
            }
        }

        public void SaveItems()
        {
            if (StoredItems != null)
            {
                for (int i = 0; i < 12; i++)
                {
                    StoredItems[i] = Slots[i].Item.Clone();
                }
            }
        }

        public override void OnInitialize()
        {
            MainPanel = new UIPanel();
            MainPanel.SetPadding(0);
            MainPanel.Left.Set(400f, 0f);
            MainPanel.Top.Set(200f, 0f);
            MainPanel.Width.Set(220f, 0f);
            MainPanel.Height.Set(190f, 0f);
            MainPanel.OnLeftMouseDown += (evt, _) => StartDragging(evt);
            MainPanel.OnLeftMouseUp += (evt, _) => StopDragging(evt);
            Append(MainPanel);
            _titleText = new UIText("收纳袋", 0.8f)
            {
                HAlign = 0f,
                Left = new StyleDimension(10f, 0f),
                Top = new StyleDimension(10f, 0f)
            };
            MainPanel.Append(_titleText);
            var closeButton = new UIText("×", 1.2f)
            {
                HAlign = 1f,
                Left = new StyleDimension(-20f, 0f),
                Top = new StyleDimension(5f, 0f)
            };
            closeButton.OnLeftClick += (evt, _) =>
            {
                ModContent.GetInstance<BundleUISystem>().ToggleBundleUI(null);
            };
            MainPanel.Append(closeButton);

            for (int i = 0; i < 12; i++)
            {
                int x = 10 + i % 4 * 48;
                int y = 40 + i / 4 * 48;

                Slots[i] = new VanillaItemSlotWrapper(ItemSlot.Context.ChestItem, 0.85f)
                {
                    Left = new StyleDimension(x, 0f),
                    Top = new StyleDimension(y, 0f),
                    ValidItemFunc = item => true
                };
                MainPanel.Append(Slots[i]);
            }
        }

        private void StartDragging(UIMouseEvent evt)
        {
            _offset = new Vector2(evt.MousePosition.X - MainPanel.Left.Pixels, evt.MousePosition.Y - MainPanel.Top.Pixels);
            _dragging = true;
        }

        private void StopDragging(UIMouseEvent evt)
        {
            _dragging = false;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (_dragging)
            {
                MainPanel.Left.Set(Main.MouseScreen.X - _offset.X, 0f);
                MainPanel.Top.Set(Main.MouseScreen.Y - _offset.Y, 0f);
                Recalculate();
            }
        }
    }
}